package com.akl.ssmspringboot;

import com.akl.ssmspringboot.utils.ExcelUtil;
import org.apache.poi.hssf.usermodel.*;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

/**
 * @author guoxy
 * @date 2019/1/11 10:47
 * @description
 */
public class ImportExcel {

    public static void main(String[] args) throws IOException {

        //获取数据
//"时间","发动机转速(rpm)","扭矩百分比(%)","发动机水温(℃)","油门踏板开度(%)","瞬时喷油量(mg/hub)","瞬时油耗(Kg/h)","平均油耗(Kg/h)","油箱液位(%)","排气流量(Kg/h)","发动机燃油温度(℃)","发动机油温度(℃)车速(Km/h)","车辆总里程(Km)","发动机运行时间(h)","尿素液位(%)","尿素温度(℃)","下游温度(℃)","下游Nox(ppm)","环境温度(℃)","电池电压(V)"
        //excel标题
        String[] title = {"时间", "发动机转速(rpm)", "扭矩百分比(%)", "发动机水温(℃)", "油门踏板开度(%)"
                , "瞬时喷油量(mg/hub)", "瞬时油耗(Kg/h)", "平均油耗(Kg/h)", "油箱液位(%)", "排气流量(Kg/h)", "发动机燃油温度(℃)"
                , "发动机油温度(℃)车速(Km/h)", "车辆总里程(Km)", "发动机运行时间(h)", "尿素液位(%)", "尿素温度(℃)", "下游温度(℃)"
                , "下游Nox(ppm)", "环境温度(℃)", "电池电压(V)"};
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        String time = sdf.format(date);
        //excel文件名
        String fileName = "OBD监控项模拟数据" + time + ".xls";
        //sheet名
        String sheetName = "OBD监控项模拟数据";

        Calendar c = Calendar.getInstance();

        //时间
        String sj =sdf.format(c.getTime());
        // 发动机转速(rpm)
        int fdjzs = randomNum(1000, 1700);
        // 扭矩百分比(%)
        int nj = randomNum(20, 80);
        // 发动机水温(℃)
        int fdjsw = randomNum(50, 85);
        // 油门踏板开度(%)
        int ymjtkd = randomNum(5, 80);
        // 瞬时喷油量(mg/hub)
        int sspyl = randomNum(30, 90);
        // 瞬时油耗(Kg/h)
        int ssyh = randomNum(6, 30);
        // 平均油耗(Kg/h)
        int pjyh = ssyh + randomNum(0, 3);
        // 油箱液位(%)
        int yxyw = randomNum(10, 98);
        // 排气流量(Kg/h)
        int pqll = randomNum(160, 800);
        // 发动机燃油温度(℃)
        int fdjrywd = randomNum(35, 40);
        // 发动机机油温度(℃)
        int fdjjywd = randomNum(85, 95);
        // 车速(Km/h)
        int cs = randomNum(10, 100);
        // 车辆总里程(Km)
        int clzlc = randomNum(100000, 150000);
        // 发动机运行时间(h)
        int fdjyxsj = randomNum(1800, 2500);
        // 尿素液位(%)
        int nsyw = randomNum(5, 98);
        // 尿素温度(℃)
        int nswd = randomNum(2, 10);
        // 下游温度(℃)
        int xywd = randomNum(150, 300);
        // 下游Nox(ppm)
        int xyn = randomNum(300, 1100);
        // 环境温度(℃)
        int hjwd = randomNum(2, 10);
        // 电池电压(V)
        int dcdy = randomNum(24, 25);

        //"时间","发动机转速(rpm)","扭矩百分比(%)","发动机水温(℃)","油门踏板开度(%)","瞬时喷油量(mg/hub)","瞬时油耗(Kg/h)",
        // "平均油耗(Kg/h)","油箱液位(%)","排气流量(Kg/h)","发动机燃油温度(℃)","发动机油温度(℃)车速(Km/h)","车辆总里程(Km)",
        // "发动机运行时间(h)","尿素液位(%)","尿素温度(℃)","下游温度(℃)","下游Nox(ppm)","环境温度(℃)","电池电压(V)"
        String[][] content = new String[200][21];
        for (int i = 0; i < 200; i++) {
            content[i][0] = sj;
            content[i][1] = fdjzs + "";
            content[i][2] = nj + "";
            content[i][3] = fdjsw + "";
            content[i][4] = ymjtkd + "";
            content[i][5] = sspyl + "";
            content[i][6] = ssyh + "";
            content[i][7] = pjyh + "";
            content[i][8] = yxyw + "";
            content[i][9] = pqll + "";
            content[i][10] = fdjrywd + "";
            content[i][11] = fdjjywd + "";
            content[i][12] = clzlc + "";
            content[i][13] = fdjyxsj + "";
            content[i][14] = nsyw + "";
            content[i][15] = nswd + "";
            content[i][16] = xywd + "";
            content[i][17] = xyn + "";
            content[i][18] = hjwd + "";
            content[i][19] = hjwd + "";
            content[i][20] = dcdy + "";
            c.add(Calendar.SECOND, 5);
            sj =sdf.format(c.getTime());
        }


        //创建HSSFWorkbook
        HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, content, null);


        //输出Excel文件
        FileOutputStream output=new FileOutputStream("d:\\workbook.xls");
        wb.write(output);
        output.flush();


    }



    /**
     * 生成min到max之间的随机数
     *
     * @param min
     * @param max
     * @return
     */
    public static int randomNum(int min, int max) {
        int num = new Random().nextInt(max - min) + min;
        return num;
    }
}
